import { gameConfigHandle } from "core/config/gameConfig"
import { BattleResult } from "core/typeDefine/typeDefine"

class ArenaHandle {
    calcScore(_player1Score: number, _player2Score: number, result: BattleResult, time: number) {
        let player1Score = this.getScore(_player1Score)
        let player2Score = this.getScore(_player2Score)
        let per1 = Math.abs(player1Score - player2Score) / Math.min(player1Score, player2Score) + 1
        let per2 = 1 - Math.abs(player1Score - player2Score) / Math.max(player1Score, player2Score)
        let addScore1 = 0, addScore2 = 0
        if (result == BattleResult.Win) {
            addScore1 = 20
            addScore2 = -10
            if (player2Score < 150) {
                addScore2 = 0
            }
            if (player1Score < player2Score) {
                addScore1 = Math.floor(addScore1 * per1)
                addScore2 = Math.floor(addScore2 * per1 * (1 + player2Score / 1000))

            } else {
                addScore1 = Math.floor(addScore1 * per2)
                addScore2 = Math.floor(addScore2 * per2)
            }
        } else {
            addScore1 = -20
            addScore2 = 10
            if (player1Score < 150) {
                addScore1 = 0
            }
            if (player2Score < player1Score) {
                addScore1 = Math.floor(addScore1 * per1 * (1 + player1Score / 1000))
                addScore2 = Math.floor(addScore2 * per1)
            } else {
                addScore1 = Math.floor(addScore1 * per2)
                addScore2 = Math.floor(addScore2 * per2)
            }
        }
        addScore1 = Math.floor(addScore1 * (2900 - player1Score) / 3000)
        addScore2 = Math.floor(addScore2 * (2900 - player2Score) / 3000)
        let newPlayer1Score = player1Score + addScore1
        let newPlayer2Score = player2Score + addScore2
        if (newPlayer1Score > 2000) {
            newPlayer1Score = 2000
        }
        if (newPlayer2Score > 2000) {
            newPlayer2Score = 2000
        }
        if (newPlayer1Score == player1Score) {
            player1Score = _player1Score
        } else {
            player1Score = this.setScore(newPlayer1Score, time)
        }
        if (newPlayer2Score == player2Score) {
            player2Score = _player2Score
        } else {
            player2Score = this.setScore(newPlayer2Score, time)
        }
        return { player1Score: player1Score, player2Score: player2Score, addScore1, addScore2 }
    }

    getScore(score: number) {
        score = score || 0
        return Math.floor(score / 100000000) || 100
    }

    setScore(score: number, time) {
        let addTime = Math.floor((2524608000000 - time) / 60000)
        return score * 100000000 + addTime
    }

    getMaxTime(time) {
        let arenaConfig = gameConfigHandle.gameConfig.arena
        let hour = new Date(time).getHours() + 1
        if (hour >= arenaConfig.maxTimes) {
            hour = arenaConfig.maxTimes
        }
        return  hour
    }
}

let arenaHandle = new ArenaHandle()
export default arenaHandle